
from xithyl.Ctypes.libc import string
from ctypes import *

variable1 = 43543
variable2 = "Telly"

def fce1():
    global variable3
    variable3 = "Included global"
    print "help"

def fce2(param):
    print param
    print range(3)
    global variable2
    variable2 = 324324324
    variable2 = param

def ffs(number, step, lower):
    res = None
    if lower:
        res = string.ffs(number >> step)
        print ">>>>", hex(number), res, hex(number >> step), "<<<<"
    else:
        res = string.ffs(number << step)
        print ">>>>", hex(number), res, hex(number << step), "<<<<"
    return res

def ffsl():
    x = 0xFFFFFFFFFFFFFFFF
    for i in range(66):
        print ">>>>", hex(x), string.ffsl(x << i), hex(x << i), "<<<<"
    for i in range(66):
        print ">>>>", hex(x), string.ffsl(x >> i), hex(x >> i), "<<<<"
    x = 0x7FFFFFFFFFFFFFFF
    for i in range(66):
        print ">>>>", hex(x), string.ffsl(x << i), hex(x << i), "<<<<"
    for i in range(66):
        print ">>>>", hex(x), string.ffsl(x >> i), hex(x >> i), "<<<<"

def ffsll():
    x = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
    for i in range(130):
        print ">>>>", hex(x), string.ffsll(x << i), hex(x << i), "<<<<"
    for i in range(130):
        print ">>>>", hex(x), string.ffsll(x >> i), hex(x >> i), "<<<<"
    x = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
    for i in range(130):
        print ">>>>", hex(x), string.ffsll(x << i), hex(x << i), "<<<<"
    for i in range(130):
        print ">>>>", hex(x), string.ffsll(x >> i), hex(x >> i), "<<<<"
    

def memccpy():
    first = c_char_p("Hello, World!")
    second = c_char_p("Another")
    ret = string.memccpy(cast(first, c_void_p), cast(second, c_void_p), 0, 50)
    print "######", hex(cast(first, c_void_p).value), hex(cast(second, c_void_p).value), hex(ret), "#######"
    print "######", hex(ret - cast(first, c_void_p).value), "#######"
    print "######", first.value, "#######"

def strcasecmp():
    s1 = "Hello"
    s2 = "HeLLo"
    ret = string.strcasecmp(s1, s2)

    print "######", s1, s2, ret, "#######"

def segfault():
    null = c_void_p(0)
    nullchar = cast(null, c_char_p)
    deadString = c_char_p("Die!")
    string.strcpy(nullchar, deadString)

